package com.zhang.generateexcel.controller;

import com.zhang.generateexcel.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

@Controller
public class Test {
    @RequestMapping("/generatePage")
    public String show() {
        return "generatePage";
    }

    @PostMapping("excel")
    public String excel(String param) throws Exception {
        Workbook workbook = new XSSFWorkbook(cn.hutool.core.io.resource.ResourceUtil.getStream("考勤模板\\考勤.xlsx"));
        FileOutputStream file = null;
        FileOutputStream file2 = null;
        //创建样式
        CellStyle cellStyle = workbook.createCellStyle();
        //文字位置：
//水平方向
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中cellStyle.setAlignment(HorizontalAlignment.LEFT);//靠左
//        cellStyle.setAlignment(HorizontalAlignment.RIGHT);//靠右
//        cellStyle.setAlignment(HorizontalAlignment.GENERAL);//常规
//        cellStyle.setAlignment(HorizontalAlignment.FILL);//填充
//        cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);//两端对齐
//        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);//跨列居中
//        cellStyle.setAlignment(HorizontalAlignment.DISTRIBUTED);//分散对齐（缩进）
//上下居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
//        cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
//        cellStyle.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);//分散对齐（缩进）
//        cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);//两端对齐
        //字体设置：

        /*⾏设置了字体后，excel中所有的⾏的字体样式都发⽣了改变 */
        Font font = workbook.createFont();
        font.setFontName("宋体");//字体
        font.setFontHeightInPoints((short) 10);//字体⾼度（与excel中的字号⼀致）
        cellStyle.setFont(font);

        //边框设置：

        //实线类
//        BorderStyle.Thin;//细实线（⿊⾊）
//        BorderStyle.Medium;//粗实线（⿊⾊）
//        BorderStyle.Thick;//更粗的实线
//        BorderStyle.Double;//细的双实线

        //虚线类
//        BorderStyle.Hair;//点状的虚线
//        BorderStyle.Dashed;//细的，较短虚线
//        BorderStyle.Dotted;//细的，短的的虚线（⽐dashed更短）
//        BorderStyle.MediumDashed;//粗的，长的短横虚线
//        BorderStyle.DashDot;//细的，长短交替的虚线
//        BorderStyle.MediumDashDot;//粗的，长短交替的虚线
//        BorderStyle.DashDotDot;//细的长短短交替的虚线
//        BorderStyle.MediumDashDotDot;//粗的，长短短交替的虚线
//        BorderStyle.SlantedDashDot;//连接较为紧密的虚线（有点花那种）

        //上下左右四种边框：
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);

        //边框颜色设置：

//        cellStyle.setBottomBorderColor();
//        cellStyle.setLeftBorderColor();
//        cellStyle.setRightBorderColor();
//        cellStyle.setTopBorderColor();
        //单元格背景颜色设置

//        cellStyle.setFillForegroundColor((short)10);//设置前景色 10为红色
//        cellStyle.setFillBackgroundColor((short)10);//设置背景色
//        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//必须设置 否则无效
        try {
            // 获取工作簿对象
            Sheet sheet = workbook.getSheetAt(0);
            String defaultDate = "";
            String year = "";
            String month = "";
            if (StringUtil.isEmpty(param)) {
                param = TimeUtil.getYearMonth();
            }
            if (StringUtil.isNotEmpty(param)) {
                if (IntegerUtil.notEquals(param.length(), 7, 6) || !StringUtil.indexOf(param, "-")) {
                    return "格式不正确，示例：2022-11";
                }
                defaultDate = param + "-01";
                year = StringUtil.split_(param, "-")[0];
                month = StringUtil.split_(param, "-")[1];
            }

            Date date = TimeUtil.fromString(defaultDate);
            Date monthEnd = TimeUtil.getMonthEnd(date);
            int dayInt = TimeUtil.getDayInt(monthEnd);
            int cell0Value = 0;
//            System.out.println(HolidaysUtil.HOLIDAY_LIST);
//            System.out.println(HolidaysUtil.SPECIAL_WORKDAY_LIST);
            int workDay = 0;
            for (int i = 9; i < 9 + dayInt; i++) {
                cell0Value++;
                //当前日期
                Date nowDate = TimeUtil.getDayPlus(date, cell0Value - 1);
                String nowDateString = TimeUtil.getDateString(nowDate);
                //星期
                int weekDay = TimeUtil.getWeekDay(nowDate);
                Row row0 = sheet.createRow(i);
                row0.setHeight((short) 282.49);
                Cell cell0 = row0.createCell(0);
                cell0.setCellValue(cell0Value);
                cell0.setCellStyle(cellStyle);
                Cell cell1 = row0.createCell(1);
                cell1.setCellValue(TimeUtil.toString(weekDay));
                cell1.setCellStyle(cellStyle);
                Cell cell2 = row0.createCell(2);
                cell2.setCellStyle(cellStyle);
                Cell cell3 = row0.createCell(3);
                cell3.setCellStyle(cellStyle);
                if (IntegerUtil.equals(weekDay, 6, 0)
                        && ObjectUtil.equals(nowDateString, HolidaysUtil.SPECIAL_WORKDAY_LIST)) {
                    cell2.setCellValue("9:00");
                    cell3.setCellValue("6:00");
                    workDay++;
                } else if (IntegerUtil.equals(weekDay, 1, 2, 3, 4, 5)
                        && ObjectUtil.notEquals(nowDateString, HolidaysUtil.HOLIDAY_LIST)) {
                    cell2.setCellValue("9:00");
                    cell3.setCellValue("6:00");
                    workDay++;
                }
                Cell cell4 = row0.createCell(4);
                cell4.setCellValue("");
                cell4.setCellStyle(cellStyle);
                Cell cell5 = row0.createCell(5);
                cell5.setCellValue("");
                cell5.setCellStyle(cellStyle);
                Cell cell6 = row0.createCell(6);
                cell6.setCellValue("");
                cell6.setCellStyle(cellStyle);
                Cell cell7 = row0.createCell(7);
                cell7.setCellValue("");
                cell7.setCellStyle(cellStyle);
                Cell cell8 = row0.createCell(8);
                cell8.setCellValue("");
                cell8.setCellStyle(cellStyle);
                Cell cell9 = row0.createCell(9);
                cell9.setCellValue("");
                cell9.setCellStyle(cellStyle);
                Cell cell10 = row0.createCell(10);
                cell10.setCellValue("");
                cell10.setCellStyle(cellStyle);
            }

            //创建样式
            CellStyle cellStyleHeader = workbook.createCellStyle();
            Font fontHeader = workbook.createFont();
            //字体
            fontHeader.setFontName("黑体");
            fontHeader.setFontHeightInPoints((short) 10);
            fontHeader.setBold(true);
            cellStyleHeader.setFont(fontHeader);
            cellStyleHeader.setBorderBottom(BorderStyle.NONE);
            cellStyleHeader.setBorderTop(BorderStyle.NONE);
            cellStyleHeader.setBorderLeft(BorderStyle.NONE);
            cellStyleHeader.setBorderRight(BorderStyle.NONE);
            //水平方向
            cellStyleHeader.setAlignment(HorizontalAlignment.CENTER);
            //垂直方向
            cellStyleHeader.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyleHeader.setBorderBottom(BorderStyle.THIN);
            Row row4 = sheet.getRow(4);
            Cell cell1 = row4.createCell(1);
            cell1.setCellValue(IntegerUtil.parseValue(year));
            cell1.setCellStyle(cellStyleHeader);
            Row row5 = sheet.getRow(5);
            Cell cell2 = row5.createCell(1);
            cell2.setCellValue(IntegerUtil.parseValue(month) + "月");
            cell2.setCellStyle(cellStyleHeader);
            Cell cell4 = row5.createCell(4);
            cell4.setCellValue(workDay);
            cell4.setCellStyle(cellStyleHeader);
            Cell cell7 = row5.createCell(7);
            cell7.setCellValue(workDay);
            cell7.setCellStyle(cellStyleHeader);
            //写入
            String path = StringUtil.format("D:\\考勤\\{}\\{}月考勤.xlsx",
                    IntegerUtil.parseValue(year), IntegerUtil.parseValue(month));
            FileUtil.makeDirs(path);
            file = new FileOutputStream(path);
            workbook.write(file);
            String path2 = StringUtil.format("C:\\Users\\32760\\Desktop\\{}月考勤.xlsx",
                    IntegerUtil.parseValue(month));
            FileUtil.makeDirs(path2);
            file2 = new FileOutputStream(path2);
            workbook.write(file2);
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        } finally {
            workbook.close();
            if (file != null) {
                file.close();
            }
            if (file2 != null) {
                file2.close();
            }
        }
        return "success";
    }
}
